package com.hoyue.attendance.WebSocket.Socket;

import com.hoyue.attendance.bean.AttendanceVo;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouyong
 * @date 2019/8/25 11:08
 * Websocket类
 */

@ServerEndpoint("/attend")
public class WebSocket {

    private static List<Session> list = new ArrayList<>();

    @OnOpen
    public void Onopen(Session session){
        System.out.println(session.getId() + " 连接进来了");
        list.add(session);
        System.out.println(list);
    }

    // 接收客户端信息
    @OnMessage
    public void onMessage(Session session,String msg){
        for(Session x : list){
            if(x.getId() == session.getId()){
                continue;
            }else {
                try {
                    x.getBasicRemote().sendText(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @OnClose
    public void OnClose(Session session){
        list.remove(session);
        System.out.println("session " + session.getId() + " 连接关闭");
    }
}
